package com.hankol.feature_wine_meal.adapter

import com.baselibrary.utils.GlideUtils
import com.baselibrary.utils.NumberUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hankol.feature_wine_meal.R
import com.hankol.feature_wine_meal.bean.FoodDetailsBean
import com.hankol.feature_wine_meal.viewmodel.WineFoodViewModel
import com.like.LikeButton
import com.like.OnLikeListener

/**
 * @author: 关忆北、
 * @date: 2022/6/13
 * @Des：
 */
class HotFoodMenuAdapter(var viewModel: WineFoodViewModel) : BaseQuickAdapter<FoodDetailsBean, BaseViewHolder>(R.layout.item_hot_food_menu) {
    override fun convert(helper: BaseViewHolder, item: FoodDetailsBean) {
        helper.setText(R.id.tv_hot_content, item.desc)
                .setText(R.id.tv_hot_major, "${item.mainFood.size}种主料")
                .setText(R.id.iv_hot_giveNum, NumberUtils.formatBigNum(item.supportNum.toString()))
        GlideUtils.loadImg_228(mContext.applicationContext, item.cover, helper.getView(R.id.round_hot_cover))
        val ivHotGive = helper.getView<LikeButton>(R.id.iv_hot_give)
        ivHotGive.isLiked = item.hasLiked == 1
        ivHotGive.setOnLikeListener(object : OnLikeListener {
            override fun liked(likeButton: LikeButton?) {
                viewModel.reqSupport(true, 9, item.id.toInt())
                item.hasLiked = 1
                item.supportNum = item.supportNum.plus(1)
                helper.setText(R.id.iv_hot_giveNum, NumberUtils.formatBigNum(item.supportNum.toString()))
            }

            override fun unLiked(likeButton: LikeButton?) {
                viewModel.reqSupport(false, 9, item.id.toInt())
                item.hasLiked = 0
                item.supportNum = item.supportNum.minus(1)
                if (item.supportNum < 0) {
                    item.supportNum = 0
                }
                helper.setText(R.id.iv_hot_giveNum, NumberUtils.formatBigNum(item.supportNum.toString()))
            }
        })

    }
}